<?php

class TakeawayOrderAction extends CommonAction{
public function index()
{
if(trim($_REQUEST['status'])!='-1'&&trim($_REQUEST['status'])!='')
{
$map['status'] = array('eq',trim($_REQUEST['status']));
}
if(intval($_REQUEST['supplier_location_id']))
{
echo $_REQUEST['supplier_location_id'];
$map['supplier_location_id'] = array('eq',intval($_REQUEST['supplier_location_id']));
}
if(trim($_REQUEST['order_sn'])!='')
{
$map['order_sn'] = array('eq',trim($_REQUEST['order_sn']));
}
if(trim($_REQUEST['begindate'])!=''&&trim($_REQUEST['enddate'])!=''){
$start_time = to_timespan(trim($_REQUEST['begindate']));
$end_time = to_timespan(trim($_REQUEST['enddate']));
$map['create_time']= array('between',array($start_time,$end_time));
}
if(trim($_REQUEST['begindate'])!=''&&trim($_REQUEST['enddate'])==''){
$start_time = to_timespan(trim($_REQUEST['begindate']));
$map['create_time']= array('egt',$start_time);
}
if(trim($_REQUEST['begindate'])==''&&trim($_REQUEST['enddate'])!=''){
$end_time = to_timespan(trim($_REQUEST['enddate']));
$map['create_time']= array('elt',$end_time);
}
if($this->get('default_map'))
$map = array_merge($map,$this->get('default_map'));
if (method_exists ( $this,'_filter')) {
$this->_filter ( $map );
}
$name=$this->getActionName();
$model =D ($name);
if (!empty ( $model )) {
$this->_list ( $model,$map );
}
$this->display ();
return;
}
public function view_order(){
$id = intval($_REQUEST ['id']);
$condition['id'] = $id;
$order_info =  M(MODULE_NAME)->where($condition)->find();
$cache_data=unserialize($order_info['cache_data']);
foreach($cache_data as $k=>$v)
{
$order_info['total_price']+=$v['price']*$v['nums'];
$order_info['order_info'].=$v['name'].'['.round($v['price'],2).']'.'×'.$v['nums'].' + ';
}
$order_info['order_info']=substr($order_info['order_info'],0,strlen($order_info['order_info'])-3);
$this->assign('order_info',$order_info);
$this->display ();
}
public function cancelbox(){
$id =  intval($_REQUEST['id']);
$this->assign('order_id',$id);
$this->display ();
}
public function do_cancel(){
$id =  intval($_REQUEST['order_id']);
$order_tip = $_REQUEST['order_tip'];
D(MODULE_NAME)->where('id='.$id)->setField(array('status','order_tip'),array(2,$order_tip));
save_log('撤销外卖!',1);
$this->success(L('UPDATE_SUCCESS'));
}
public function export_csv($page = 1){
set_time_limit(0);
$limit = (($page -1)*intval(app_conf('BATCH_PAGE_SIZE'))).','.(intval(app_conf('BATCH_PAGE_SIZE')));
if(!isset($_REQUEST['status']))
{
$_REQUEST['status'] = -1;
}
$where = ' 1=1 ';
if(intval($_REQUEST['supplier_location_id']))
{
$where.=' and supplier_location_id='.intval($_REQUEST['supplier_location_id']).'';
}
if(trim($_REQUEST['order_sn'])!='')
{
$where.= ' and '.DB_PREFIX."takeaway_order.order_sn like '%".trim($_REQUEST['order_sn'])."%' ";
}
if(trim($_REQUEST['begindate'])!=''&&trim($_REQUEST['enddate'])!=''){
$start_time = to_timespan(trim($_REQUEST['begindate']));
$end_time = to_timespan(trim($_REQUEST['enddate']));
$where.= ' and '.DB_PREFIX."takeaway_order.create_time between $start_time and $end_time";
}
if(trim($_REQUEST['begindate'])!=''&&trim($_REQUEST['enddate'])==''){
$start_time = to_timespan(trim($_REQUEST['begindate']));
$where.= ' and '.DB_PREFIX."takeaway_order.create_time >= $start_time";
}
if(trim($_REQUEST['begindate'])==''&&trim($_REQUEST['enddate'])!=''){
$end_time = to_timespan(trim($_REQUEST['enddate']));
$where.= ' and '.DB_PREFIX."takeaway_order.create_time <= $end_time ";
}
$list = M('TakeawayOrder')
->where($where)
->field(DB_PREFIX.'takeaway_order.*')
->limit($limit)->findAll ( );
if($list){
register_shutdown_function(array(&$this,'export_csv'),$page+1);
$order_value=array('sn'=>'""','supplier_location_name'=>'""','order_info'=>'""','status'=>'""','create_time'=>'""','order_price'=>'""','pay_price'=>'""','user_name'=>'""','contacter'=>'""','tel'=>'""','address'=>'""','order_tip'=>'""','msg'=>'""',);
if($page==1)
{
$content=iconv('utf-8','gbk','订单号,商户,订单信息,订单状态,下单时间,订单总额,消费总额,会员,联系人,联系电话,地址,失败原因');
$content.="\n";
}
foreach($list as $k=>$v)
{
$order_value['sn'] = '"'.'sn:'.iconv('utf-8','gbk',$v['order_sn']) .'"';
$supplier_location = M('SupplierLocation')->getById($v['supplier_location_id']);
$order_value['supplier_location_name'] = '"'.iconv('utf-8','gbk',$supplier_location['name']) .'"';
$total_price=0;
$order_info='';
$cache_data=unserialize($v['cache_data']);
foreach($cache_data as $kk=>$vv)
{
$total_price+=$vv['price']*$vv['nums'];
$order_info.=$vv['name'].'['.round($vv['price'],2).']'.'×'.$vv['nums'].' + ';
}
$order_info=substr($order_info,0,strlen($order_info)-3);
$order_value['order_info'] = '"'.iconv('utf-8','gbk',$order_info) .'"';
$order_value['order_price'] = '"'.iconv('utf-8','gbk',format_price($total_price)) .'"';
$order_value['pay_price']=0;
if($v['status']==1)
$order_value['pay_price'] = '"'.iconv('utf-8','gbk',format_price($total_price)) .'"';
$order_value['status'] = '"'.iconv('utf-8','gbk',get_order_status($v['status'])) .'"';
$order_value['create_time'] = '"'.iconv('utf-8','gbk',to_date($v['create_time'])) .'"';
$user_info = M('User')->getById($v['user_id']);
$order_value['user_name'] = '"'.iconv('utf-8','gbk',$user_info['user_name']) .'"';
if($v['sex']==1)
$order_value['contacter'] = '"'.iconv('utf-8','gbk',$v['contacter'].'先生') .'"';
else
$order_value['contacter'] = '"'.iconv('utf-8','gbk',$v['contacter'].'女士') .'"';
$order_value['tel'] = '"'.iconv('utf-8','gbk',$v['tel']) .'"';
$order_value['address'] = '"'.iconv('utf-8','gbk',$v['address']) .'"';
$order_value['order_tip'] = '"'.iconv('utf-8','gbk',$v['order_tip']) .'"';
$order_value['msg'] = '"'.iconv('utf-8','gbk',$v['msg']) .'"';
$content .= implode(',',$order_value) ."\n";
}
header('Content-Disposition: attachment; filename=takeaway_order_list.csv');
echo $content;
}
else
{
if($page==1)
$this->error(L('NO_RESULT'));
}
}
}
function get_order_status($status){
return l('STORE_ORDER_TAKEAWAY_STATUS_'.$status);
}
function get_location_name($sid){
return M('SupplierLocation')->where('id='.$sid)->getField('name');
}
function get_total_price($cache_data,$status){
$cache_data=unserialize($cache_data);
if($status==1)
{
foreach($cache_data as $k=>$v)
{
$total_price+=$v['price']*$v['nums'];
}
}
return $total_price;
}
?>